package org.saxonextensions.eml;

import junit.framework.TestCase;

import javax.xml.transform.*;
import java.util.Map;
import java.io.File;

import net.sf.saxon.Controller;

/**
 * Abstract test case
 *
 * @author Ivan Latysh <ivan@yourmail.com>
 * @version 0.1
 * @since 5-May-2008 2:10:45 PM
 */
public abstract class AbstractTestCase extends TestCase {

  /* Transformer factory */
  public final TransformerFactory factory = TransformerFactory.newInstance();

  /** Home dir */
  public File home = new File(System.getProperty("user.dir"));

  /**
   * Execute transformation
   *
   * @param xslSource XSL source
   * @param xmlSource XML source
   * @param props additional properties
   * @param result result
   * @throws TransformerException when transformation failed 
   */
  public void transform(Source xslSource, Source xmlSource, Map<String, Object> props, Result result) throws TransformerException {
    // get a new transformer for given XSLT
    Transformer transformer = factory.newTransformer(xslSource);
    if (null!=props && props.size()>0) {
      // add properties
      for (Map.Entry<String, Object> _p : props.entrySet() ) {
        transformer.setParameter(_p.getKey(), _p.getValue());
      }
    }
    // register output URI Resolver
/*
    if (transformer instanceof Controller) {
      ((Controller)transformer).setOutputURIResolver();
    }
*/
    // transformer.setURIResolver();
    // perform the transformation
    transformer.transform(xmlSource, result);
  }

}
